package xiaoqi.springboot.boot.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {


    @Primary
    @Bean(name = "test2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSourceProperties test1DataSourceProperties() {
        return new DataSourceProperties();
    }


    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2.hikari")
    public DataSource test1DataSource() {
        DataSourceProperties dataSourceProperties = this.test1DataSourceProperties();
        return createHikarDataSource(dataSourceProperties);
    }

    @Bean(name = "testDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.test")
    public DataSourceProperties testDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "testDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test.hikari")
    public DataSource testDataSource() {
        DataSourceProperties dataSourceProperties = this.testDataSourceProperties();
        return createHikarDataSource(dataSourceProperties);
    }

    @Bean(name = "test3DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.test3")
    public DataSourceProperties test3DataSourceProperties() {
        return new DataSourceProperties();
    }


    @Bean(name = "test3DataSource")
    public DataSource test3DataSource() {
        return createHikarDataSource(test3DataSourceProperties());
    }

    private static HikariDataSource createHikarDataSource(DataSourceProperties properties) {
        HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        return dataSource;
    }
}
